package com.company.psm.serviceimpl;

import java.util.ArrayList;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.company.psm.entity.Bill;
import com.company.psm.entity.Medicine;
import com.company.psm.entity.Store;
import com.company.psm.entity.User;
import com.company.psm.genericdao.CustomGenericDAO;
import com.company.psm.service.BillManagement;

@Service(value="billManagement")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class BillManagementImpl extends CustomGenericDAO<Object> implements BillManagement{
	@Transactional(readOnly = true)
	public List<Bill> getAllBills() {
		// TODO Auto-generated method stub
		return _all(Bill.class);
	}
	
	@Transactional(readOnly = false)
	public Bill getBillByIdAndStore(long id, long storeId) {
		return null;
	}
	public Object find(Long id) {
		// TODO Auto-generated method stub
		return null;
	}
	public Object[] find(Long... ids) {
		// TODO Auto-generated method stub
		return null;
	}
	public Object getReference(Long id) {
		// TODO Auto-generated method stub
		return null;
	}
	public Object[] getReferences(Long... ids) {
		// TODO Auto-generated method stub
		return null;
	}
	public boolean removeById(Long id) {
		// TODO Auto-generated method stub
		return false;
	}
	public void removeByIds(Long... ids) {
		// TODO Auto-generated method stub
		
	}
	public void deleteBill(long billId) {
		// TODO Auto-generated method stub
		
	}
	@Transactional(readOnly = false)
	public void createBill(String billDate, String storeName,
			String billNumber, String saleStaffName, String customerName,
			List<Medicine> medicines, String totalValue, Store store,
			String description, User user) {
		Bill bill = new Bill();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = (Date) df.parse(billDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		bill.setBillDate(date);
		bill.setStoreName(storeName);
		bill.setBillNumber(billNumber);
		bill.setSaleStaffName(saleStaffName);
		bill.setCustomerName(customerName);
		bill.setTotalValue(totalValue);
		bill.setStore(store);
		bill.setDescription(description);
		bill.setUser(user);
		_save(bill);
	}

	public List<Bill> searchBillByCustomerDate(String customerName,
			Long storeId,Date fromDate, Date toDate, User user, String userId) {
		List<Bill> allBill = _all(Bill.class);
		List<Bill> listBill = new ArrayList<Bill>();
		if(user.getRole().getCode().equals("ROLE_STORE_OWNER")){
			if(fromDate !=null){
				for (Bill bill : allBill) {
					if(fromDate!= null || toDate !=null){
						if(bill.getStore().getId() == storeId && bill.getCustomerName().contains(customerName)
							&&bill.getBillDate().compareTo(fromDate) >= 0 && bill.getBillDate().compareTo(toDate) <= 0){
								if(userId!=null && !userId.equals("")){
									if(bill.getUser().getId() == Long.parseLong(userId)){
										listBill.add(bill);
									}
								}
								else{
									listBill.add(bill);
								}
						}
					}
				}
			}
			else{
				for (Bill bill : allBill) {
					if(fromDate!= null || toDate !=null){
						if(bill.getStore().getId() == storeId && bill.getCustomerName().contains(customerName)
							&& bill.getBillDate().compareTo(toDate) <= 0){
							if(userId!=null && !userId.equals("")){
								if(bill.getUser().getId() == Long.parseLong(userId)){
									listBill.add(bill);
								}
							}
							else{
								listBill.add(bill);
							}
						}
					}
				}
			}
		}else{
			if(fromDate !=null){
				for (Bill bill : allBill) {
					if(fromDate!= null || toDate !=null){
						if(bill.getStore().getId() == storeId && bill.getCustomerName().contains(customerName)
							&&bill.getBillDate().compareTo(fromDate) >= 0 && bill.getBillDate().compareTo(toDate) <= 0 &&bill.getUser().getId()==user.getId()){
								listBill.add(bill);
							}
						}
					}
				}
			else{
				for (Bill bill : allBill) {
					if(fromDate!= null || toDate !=null){
						if(bill.getStore().getId() == storeId && bill.getCustomerName().contains(customerName)
							&& bill.getBillDate().compareTo(toDate) <= 0&&bill.getUser().getId()==user.getId()){
								listBill.add(bill);
							}
						}
					}
				}
		}
		return listBill;
	}

	public List<Bill> searchBillByDateAndStore(Date fromDate,
			Date toDate, Long storeId) {
		List<Bill> allBill = _all(Bill.class);
		List<Bill> listBill = new ArrayList<Bill>();
		for (Bill bill : allBill) {
			if(bill.getStore().getId() == storeId && bill.getBillDate().compareTo(fromDate) >= 0 && bill.getBillDate().compareTo(toDate) <= 0){
				listBill.add(bill);
			}
		}
		return listBill;
	}

	public List<Bill> getAllBillByStore(long storeId) {
		List<Bill> allBill = _all(Bill.class);
		List<Bill> listBill = new ArrayList<Bill>();
		for (Bill bill : allBill) {
			if(bill.getStore().getId() == storeId){
				listBill.add(bill);
			}
		}
		return listBill;
	}

	public List<Bill> getAllBillUser(long userId) {
		List<Bill> allBill = _all(Bill.class);
		List<Bill> listBill = new ArrayList<Bill>();
		for (Bill bill : allBill) {
			if(bill.getUser().getId() == userId){
				listBill.add(bill);
			}
		}
		return listBill;
	}
}
